// pages/itemsDetail/itemsDetail.js
const unit = require("../../utils/util")
var WxParse = require("../../utils/wxParse/wxParse")
Page({

  /**
   * 页面的初始数据
   */
  data: {
		goodsId:"",
    itemId: 0,
    value: 1,
    num: "",
    show: false,
    shows: false,
    image: {},
    information: [],
    attribute: [],
    product: [],
    specificationList: {},
    sunlist: [],
    productone: {}
  },

  // 选中
  Selectedfun(e) {
    let index1 = e.currentTarget.dataset.index1
    let index2 = e.currentTarget.dataset.index2+1
    if (this.data.sunlist[index1] == 0 || this.data.sunlist[index1] != index2) {
      this.setData({
        [`sunlist[${index1}]`]: index2
      })
    } else {
      this.setData({
        [`sunlist[${index1}]`]: 0
      })
    }
    if (this.data.sunlist.findIndex(item => item == 0) == -1) {
      // for (let index = 0; index < this.data.product.length; index++) {
        // console.log(this.data.product[index]);
      // }
      this.data.product.map(item=>{
        // console.log(item);
        if(this.data.sunlist.length>1){
          if(item.specivalue[0] == this.data.sunlist[0] & item.specivalue[1] == this.data.sunlist[1]){
						// console.log(item.goodsId);
						this.setData({
							goodsId:item.goodsId
						})
          }
        }else{
					this.setData({
						goodsId:item.goodsId
					})
          
            // console.log(item.id);
            // console.log(item.goodsId);
          
        }
       
        // item.specivalue[index] = 
      })

    }
  },

  getList() {
    unit.axios({
      url: "goods/detail",
      data: {
        id: this.data.itemId
      },
      method: "GET"
    }).then((res) => {
      console.log(res);
      this.setData({
        image: res.data.data.info.gallery,
        information: res.data.data.info,
        attribute: res.data.data.attribute,
        product: res.data.data.productList,
        specificationList: res.data.data.specificationList,
        info: res.data.data.info,
        goodsId:res.data.data.info.id
      })
      for (let index = 0; index < this.data.specificationList.length; index++) {
        this.setData({
          [`sunlist[${index}]`]: 0,
        })
      }
      let num = 0
      for (let index1 = 0; index1 < this.data.specificationList[0].valueList.length; index1++) {
        if (this.data.specificationList[1]) {
          for (let index2 = 0; index2 < this.data.specificationList[1].valueList.length; index2++) {
            this.setData({
              [`product[${num}].specivalue`]: [index1 + 1, index2 + 1]
            })
            num++
          }
        }else{
          this.setData({
            [`product[${num}].specivalue`]: [index1 + 1]
          })
          num++
        }
      }

      let that = this;
      WxParse.wxParse('goodsDetail', 'html', res.data.data.info.detail, that, 0);
      console.log(this.data.product);
    })
  },

  getGoodsCount() {
    unit.axios({
      url: "/cart/goodscount",
      data: {},
      method: "GET"
    }).then((res) => {
      console.log(res);
      this.setData({
        num: res.data.data
      })
    })
  },

  getGoods() {
		if (this.data.sunlist.findIndex(item => item == 0) == -1) {
    unit.axios({
      url: "/cart/fastadd",
      data: {
        goodsId: this.data.goodsId,
        number: this.data.value,
        productId: this.data.product[0].id
      },
      method: "POST",
      header: {
        "X-Byesame-Token": wx.getStorageSync('token')
      }
    }).then((res) => {
      if (res.data.errno == 501) {
        wx.reLaunch({
          url: '../login/login'
        })
      } else if (res.data.errno == 0) {
        console.log(res);
        this.setData({
          num: res.data.data
				})
			
        wx.navigateTo({
          url: '../orderCheckout/orderCheckout?id=' + this.data.num
        })
        // console.log(this.data.count);
      } else {
        console.log("未知错误");
      }

		})
	}
  },

  addCar(){
		if (this.data.sunlist.findIndex(item => item == 0) == -1) {
    unit.axios({
      url: "/cart/add",
      data: {
        goodsId: this.data.goodsId,
        number: this.data.value,
        productId: this.data.product[0].id
      },
      method: "POST",
      header: {
        "X-Byesame-Token": wx.getStorageSync('token')
      }
    }).then((res) => {
      if (res.data.errno == 501) {
        wx.reLaunch({
          url: '../login/login'
        })
      } else if (res.data.errno == 0) {
        console.log(res);
        this.setData({
          num:res.data.data
        })
        wx.showToast({
          title: '加入购物车成功',
          icon:"none"
        })
        this.onCloses()
      }

		})
	}
  },

  goOrder(){
    wx.switchTab({
      url: '/pages/order/order',
    })
  },


  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  showPopups() {
    this.setData({
      shows: true
    });
  },

  onCloses() {
    this.setData({
      shows: false
    });
  },

  onChange(event) {
    // console.log(event.detail);
    this.setData({
      value: event.detail
    })
  },
tologin(){
	unit.getitime()
	unit.getendtime()
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log("我是desc", options);
    this.setData({
      itemId: options.id
    })
    this.getList()
    this.getGoodsCount()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})